#pragma once
#include <assert.h>

namespace lh
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		
		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		T& front()
		{
			assert(size() > 0);
			return *_start;
		}

		T& back()
		{
			assert(size() > 0);
			return *(_finish - 1);
		}

		// construct and destroy

		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			;
		}

		vector(size_t n, const T& value = T())
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			reserve(n);
			while (n--)
			{
				push_back(value);
			}
		}

		vector(int n, const T& value = T())
			:_start(new T[n])
			, _finish(_start + n)
			, _end_of_storage(_finish)
		{
			for (int i = 0; i < n; i++)
			{
				_start[i] = value;
			}
		}

		template<class InputIterator>

		vector(InputIterator first, InputIterator last)
		{
			while (first != end)
			{
				push_back(*first);
				++first;
			}
		}

		vector(const vector<T>& v)
		{
			vector<T> tmp(v.begin(), v.end());
			swap(tmp);
		}

		vector<T>& operator= (vector<T> v)
		{
			swap(v);
			return *this;
		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}

		// capacity

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				//memcpy(tmp,_start,sizeof(T)*sz);可能导致浅拷贝，若为vector<vector<int>>类型，里面数据为vector<int>，造成浅拷贝，一块空间释放2次。
				size_t sz = size();
				T* tmp = new T[n];
				if (_start)
				{
					for (size_t i = 0; i < sz; i++)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_end_of_storage = _start + n;
			}
		}

		void resize(size_t n, const T& value = T())
		{
			if (n > capacity())
			{
				//扩容
				reserve(n);
			}
			if (n > size())
			{
				//初始化添值
				while (_finish < _start + n)
				{
					*_finish = value;
					_finish++;
				}
			}
			else
			{
				_finish = n;
			}
		}



		///////////////access///////////////////////////////

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

		const T& operator[](size_t pos)const
		{
			assert(pos < size());
			return _start[pos];
		}



		///////////////modify/////////////////////////////

		void push_back(const T& x)
		{
			insert(end(), x);
		}

		void pop_back()
		{
			assert(_finish > _start);
				_finish--;
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);
			if (_finish == _end_of_storage)
			{
				//记录pos距离_start的长度
				size_t len = pos - _start;
				//扩容
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				//深拷贝的_start+pos距离长度即为pos位置。
				pos = _start + len;
			}
			//挪数据
			iterator end = _finish - 1;
			while (end > pos)
			{
				*(end + 1) = *end;
				end--;
			}
			*pos = x;
			++_finish;
			return pos;
		}

		//规定erase返回删除位置下一个位置迭代器

		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);
			iterator begin = pos + 1;
			while (begin < _finish)
			{
				*(begin - 1) = *begin;
				++begin;
			}
			--_finish;
			return pos;
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _end_of_storage;
	};

	void Func(const vector<int>& v)
	{
		vector<int>::const_iterator it = v.begin();
		while (it != v.end())
		{
			//*it = 10;
			std::cout << *it << " ";
			++it;
		}
		std::cout << std::endl;

		for (auto e : v)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;
	}

	void test_vector1()
	{
		/*double d = 2.2;
		const int& i = d;

		vector<string> v;
		v.push_back("xxxxx");*/

		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);

		for (size_t i = 0; i < v.size(); ++i)
		{
			std::cout << v[i] << " ";
		}
		std::cout << std::endl;

		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			std::cout << *it << " ";
			++it;
		}
		std::cout << std::endl;

		v.pop_back();
		v.pop_back();

		for (auto e : v)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;
	}

	void test_vector2()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		//v.push_back(5);

		for (auto e : v)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;

		auto p = std::find(v.begin(), v.end(), 3);
		if (p != v.end())
		{
			// 在p位置插入数据以后，不要访问p，因为p可能失效了。
			v.insert(p, 30);

			//cout << *p << endl;
			//v.insert(p, 40);
		}

		for (auto e : v)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;

		v.insert(v.begin(), 1);
	}

	void test_vector3()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		for (auto e : v)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;

		auto p = std::find(v.begin(), v.end(), 3);
		if (p != v.end())
		{
			v.erase(p);
		}

		for (auto e : v)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;

		v.erase(v.begin());

		for (auto e : v)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;
	}

	//void test_vector4()
	//{
	//	 正常运行
	//	vector<int> v;
	//	v.push_back(1);
	//	v.push_back(2);
	//	v.push_back(3);
	//	v.push_back(4);
	//	v.push_back(5);

	//	 要求删除所有的偶数
	//	auto it = v.begin();
	//	while (it != v.end())
	//	{
	//		if (*it % 2 == 0)
	//		{
	//			v.erase(it);
	//		}

	//		++it;
	//	}

	//	for (auto e : v)
	//	{
	//		std::cout << e << " ";
	//	}
	//	std::cout << std::endl;
	//}

	//void test_vector4()
	//{
	//	// 崩溃
	//	vector<int> v;
	//	v.push_back(1);
	//	v.push_back(2);
	//	v.push_back(3);
	//	v.push_back(4);

	//	// 要求删除所有的偶数
	//	auto it = v.begin();
	//	while (it != v.end())
	//	{
	//		if (*it % 2 == 0)
	//		{
	//			v.erase(it);
	//		}
	//		++it;
	//	}

	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//}

	//void test_vector4()
	//{
	//	// 结果不对
	//	vector<int> v;
	//	v.push_back(1);
	//	v.push_back(2);
	//	v.push_back(4);
	//	v.push_back(3);
	//	v.push_back(4);
	//	v.push_back(5);

	//	// 要求删除所有的偶数
	//	auto it = v.begin();
	//	while (it != v.end())
	//	{
	//		if (*it % 2 == 0)
	//		{
	//			v.erase(it);
	//		}
	//			
	//		++it;
	//	}

	//	for (auto e : v)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//}

	// 正确的方式
	void test_vector4()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(4);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);

		// 要求删除所有的偶数
		auto it = v.begin();
		while (it != v.end())
		{
			if (*it % 2 == 0)
			{
				it = v.erase(it);
			}
			else
			{
				++it;

			}
		}

		for (auto e : v)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;
	}
}